package com.sydsen.imager.elements;

import com.sydsen.imager.ImageOptions;
import com.sydsen.imager.util.ImageUtils;

import java.awt.image.BufferedImage;

/**
 * @Auther: syh
 * @Date: 2021/11/1
 * @Description:
 */
public class Image extends Element<Image> {

    private BufferedImage image;
    /**
     * 图片圆角
     */
    private int radius;
    /**
     * 图片缩放
     */
    private ImageOptions.Zoom zoom = ImageOptions.Zoom.Origin;
    /**
     * 图片噪点
     */
    private int noise;

    /**
     * 翻转，默认原图
     */
    private ImageOptions.Flip flip = ImageOptions.Flip.DEFAULT;

    /**
     * 去色
     */
    private boolean gray = false;

    public Image(String imgUrl) throws Exception {
        this(imgUrl, 0, 0);
    }

    /**
     * @param imgUrl 图片url
     * @param x      x坐标
     * @param y      y坐标
     */
    public Image(String imgUrl, int x, int y) throws Exception {
        this(ImageUtils.read(imgUrl), x, y);
    }

    public Image(BufferedImage image) {
        this(image, 0, 0);
    }

    /**
     * @param image 图片对象
     * @param x     x坐标
     * @param y     y坐标
     */
    public Image(BufferedImage image, int x, int y) {
        this(image, x, y, image.getWidth(), image.getHeight());
    }

    /**
     * @param imgUrl   图片url
     * @param x        x坐标
     * @param y        y坐标
     * @param width    宽度
     * @param height   高度
     */
    public Image(String imgUrl, int x, int y, int width, int height) throws Exception {
        this(ImageUtils.read(imgUrl), x, y, width, height);
    }

    /**
     * @param image   图片url
     * @param x        x坐标
     * @param y        y坐标
     * @param width    宽度
     * @param height   高度
     */
    public Image(BufferedImage image, int x, int y, int width, int height) {
        this.image = image;
        super.width(image.getWidth());
        super.height(image.getHeight());
        super.x(x);
        super.y(y);
    }

    public BufferedImage getImage () {
        return this.image;
    }

    public int radius() {
        return radius;
    }

    public Image radius(int radius) {
        this.radius = radius;
        return this;
    }

    public ImageOptions.Zoom zoom() {
        return zoom;
    }

    public Image zoom(ImageOptions.Zoom zoom) {
        this.zoom = zoom;
        return this;
    }

    public int noise() {
        return noise;
    }

    public Image noise(int noise) {
        this.noise = noise;
        return this;
    }

    public ImageOptions.Flip flip() {
        return flip;
    }

    public Image flip(ImageOptions.Flip flip) {
        this.flip = flip;
        return this;
    }

    public boolean gray() {
        return gray;
    }

    public Image gray(boolean gray) {
        this.gray = gray;
        return this;
    }
}
